//echarts注册地图所需json
	var allJson = '{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"name":"${name}"},"geometry":{"type":"Polygon","coordinates":${coordinates}}}],"UTF8Encoding":false}';


	// 百度地图API
	var map = new BMap.Map("map");
	map.enableScrollWheelZoom();
	var bdary = new BMap.Boundary();
	view();


	function view(type){
		var area = document.getElementById("area").value;
		if(!area){
			alert("请输入区域");
			return;
		}
	   
	    bdary.get(area, function(rs){ 
	        map.clearOverlays();        //清除覆盖物
	        var content = rs.boundaries
	        if (!content||!content.length) {
	            alert('未能获取当前输入行政区域');
	            return ;
	        }
	        var pointArray = [];
		    var length = content.length; 
		    for (var i = 0; i < length; i++) {
	            var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 3, strokeColor: "red"}); //画出轮廓
	            map.addOverlay(ply);  //将轮廓添加到地图
	            pointArray = pointArray.concat(ply.getPath());
	        }    
	        map.setViewport(pointArray);  //调整视野范围     

	    	if(type){//下载
		        //拼接json数据
		        var jsonArr=[];
		        for(var i in content){
		        	var json = "[["+content[i].replace(/;/g,"],[")+"]]";
		        	jsonArr.push(json);
		        }
		        var coordinateStr = "["+jsonArr.join(",")+"]";
		        allJson = allJson.replace("${name}",area).replace("${coordinates}",coordinateStr);
		       	if("json"==type){
		       		var fileName = area+'.json';
			        download(fileName,allJson);
		       	}
		       	if("html"==type){
		       		var xmlHttp = new XMLHttpRequest();
	              	xmlHttp.onreadystatechange = function() {
	                    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
	                    	var html = xmlHttp.responseText;
							html=html.replace(/\${name}/g,area).replace(/\${json}/g,allJson);
							var fileName = area+'.html';
							download(fileName,html);
	                    }
	              	}
					xmlHttp.open("GET", "html/demo.html",true);
					xmlHttp.send();
		       	}
	    	}
	    });
	}
	
	//下载文件	
	function download(fileName,content){
		var blob = new Blob([content],{ type: 'application/octet-stream;charset=UTF-8' });
		if(!!window.ActiveXObject || "ActiveXObject" in window){
			window.navigator.msSaveOrOpenBlob(blob,fileName);
		}else{
			var evt = document.createEvent("HTMLEvents");
		    evt.initEvent("click", false, false);
		    var aLink = document.createElement('a');
		    aLink.download = fileName;
		    aLink.href = URL.createObjectURL(blob);
		    aLink.dispatchEvent(evt);
		    aLink.click(); 
		}
		
	}
